<template>

  <!--

    对话框（2）：选择物料

    -->
  <el-dialog v-dialogDrag v-dialogDragWidth :title="dialog_selectOrderDetail.title"
             :visible.sync="dialog_selectOrderDetail.visible"
             width="1100px">
    <!-- 检索条件 -->
    <el-card class="box-card" shadow="hover" style="margin-bottom: 20px;">
      <div slot="header" class="clearfix">
        <span><b>订单基本信息</b></span>
        <el-tooltip content="显示 / 隐藏" placement="top" style="float: right; padding: 3px 0">
          <el-switch
            v-model="fold_form_order"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="false"
            inactive-value="true">
          </el-switch>
        </el-tooltip>
      </div>
      <div v-show="fold_form_order== 'false'">
        <el-form :model="dialog_selectOrderDetail.queryParams"
                 :inline="true" v-show="dialog_selectOrderDetail.showSearch" label-width="68px">
          <!-- 订单编号 -->
          <el-form-item label="客户名称" prop="custName">
            <el-input
              v-model="dialog_selectOrderDetail.queryParams.custName"
              placeholder="请输入客户名称"
              clearable
              size="small"
              @keyup.enter.native="getList_orderDetails"
            />
          </el-form-item>
          <!-- 订单名称 -->
          <el-form-item label="物料名称" prop="itemName">
            <el-input
              v-model="dialog_selectOrderDetail.queryParams.itemName"
              placeholder="请输入物料名称"
              clearable
              size="small"
              @keyup.enter.native="getList_orderDetails"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini"
                       @click="getList_orderDetails">搜索</el-button>
          </el-form-item>
        </el-form>
        <!-- 订单列表 -->
        <el-table v-loading="loading" :data="dialog_selectOrderDetail.oderList" row-key="itemUuid" @selection-change="handleSelectionChange">
          <el-table-column label="订单uuid" align="center" prop="orderUuid" v-if="show1" />
          <el-table-column label="订单时间" align="center" prop="docDt" width="150"/>
          <el-table-column label="销售号" align="center" prop="docNum" width="150" />
          <el-table-column label="客户名称" align="center" prop="custName" width="180" />
          <el-table-column label="物料名称" align="center" prop="itemName" width="120" :show-overflow-tooltip="true"/>
          <el-table-column label="计量单位" align="center" prop="wmsDictMeasuringUnit" width="100">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.wms_dict_measuring_unit" :value="scope.row.wmsDictMeasuringUnit"/>
            </template>
          </el-table-column>
          <el-table-column label="数量" align="center" prop="orderDetailQty" width="120"/>

          <el-table-column label="单价" align="center" prop="orderDetailAmt" width="150" >
            <template slot-scope="scope">
              {{ numFormat_thousandsSeparator(calculate(scope.row.orderDetailAmt)) }}
            </template>
          </el-table-column>
          <el-table-column label="含税单价" align="center" prop="unitPriceTaxIncluded" width="120"/>
          <el-table-column label="不含税金额" align="center" prop="orderDetailTotalAmt" width="150">
            <template slot-scope="scope">
              {{ numFormat_thousandsSeparator(calculate(scope.row.orderDetailTotalAmt)) }}
            </template>
          </el-table-column>
          <el-table-column label="税率(%)" align="center" prop="taxRate" width="120"/>
          <el-table-column label="税额" align="center" prop="taxAmt" width="150">
            <template slot-scope="scope">
              {{ numFormat_thousandsSeparator(calculate(scope.row.taxAmt)) }}
            </template>
          </el-table-column>
          <el-table-column label="价税合计" align="center" prop="totalPriceAndTax" width="150" >
            <template slot-scope="scope">
              {{ numFormat_thousandsSeparator(calculate(scope.row.totalPriceAndTax)) }}
            </template>
          </el-table-column>

          <el-table-column label="客户编号" align="center" prop="custNum" width="180"/>
          <el-table-column label="销售人员" align="center" prop="salesPerson" width="120"/>
          <el-table-column label="交付日期" align="center" prop="orderDeliveryDate" width="120" >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.orderDeliveryDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="金额(￥)" align="center" prop="orderAmt" width="120" >
            <template slot-scope="scope">
              {{ numFormat_thousandsSeparator(fun(scope.row.orderAmt)) }}
            </template>
          </el-table-column>
          <el-table-column label="订单状态" align="center" prop="dicSysNormalDisable">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.dicSysNormalDisable"/>
            </template>
          </el-table-column>
          <el-table-column label="审核状态" align="center" prop="oaDictConfirmStatus" >
            <template slot-scope="scope">
              <dict-tag :options="dict.type.oa_dict_confirm_status" :value="scope.row.oaDictConfirmStatus"/>
            </template>
          </el-table-column>
          <el-table-column label="订单完成日期" align="center" prop="stockInDocDt" width="120" >
            <template slot-scope="scope">
              <span v-if="scope.row.stockInDocDt">{{ parseTime(scope.row.stockInDocDt, '{y}-{m}-{d}') }}</span>
              <el-tag type="info" size="mini" v-show="!scope.row.stockInDocDt">未完成</el-tag>
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
            <template slot-scope="scope">
              <el-button
                type="success" plain
                size="small"
                icon="el-icon-check"
                @click="selectOrder_done(scope)"
                v-hasPermi="['crm:order:check']"
              >选择</el-button>
            </template>
          </el-table-column>
        </el-table>

      </div>
      <pagination
        v-show="dialog_selectOrderDetail.total>0"
        :total="dialog_selectOrderDetail.total"
        :page.sync="dialog_selectOrderDetail.queryParams.pageNum"
        :limit.sync="dialog_selectOrderDetail.queryParams.pageSize"
        @pagination="getList_orderDetails"
      />
    </el-card>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialog_hide_selectOrderDetail">取 消</el-button>
    </div>

  </el-dialog>

</template>

<script>
import {listCrmOrders,listOrderDetails} from "../../../api/crm/crmOrders";
import {numFormat_thousandsSeparator} from "@/utils/num";

export default {
  dicts: ['sys_normal_disable','crm_dict_urgency_level','wms_dict_measuring_unit'],
  props: {
    // 角色编号
    roleId: {
      type: [Number, String]
    }
  },
  data() {
    return {
      dialog_selectOrderDetail: {
        visible: false,
        showSearch: true,
        title: "请选择物料",
        total: 0,
        oderList: [],
        queryParams:{
          pageNum: 1,
          pageSize: 10,
          itemName: null,
          oaDictConfirmStatus: null,
        }
      },
      orderDetailList:[],
      fold_form_order: 'false',
      // 遮罩层
      loading: true,
      show1: false,
      // 遮罩层
      visible: false,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roleId: undefined,
        userName: undefined,
        phonenumber: undefined,
        orderUuid: undefined
      },
      selectedUser: {
        id: null,
        nickName: null,
        userName: null
      }
    };
  },
  methods: {

    //保留两位小数
    fun(val) {
      return Number(val).toFixed(2);
    },
    //保留六位小数
    calculate(val) {
      return Number(val).toFixed(6);
    },
    numFormat_thousandsSeparator,

    /** 读取列表：订单 */
    getList_orderDetails() {
      this.loading = true;
      this.dialog_selectOrderDetail.queryParams.oaDictConfirmStatus = 'pass'
      listCrmOrders(this.dialog_selectOrderDetail.queryParams).then(response => {
        console.log( response )
        this.dialog_selectOrderDetail.oderList = response.rows;
        this.dialog_selectOrderDetail.total = response.total;
        this.loading = false;
      });
    },

// 多选框选中数据
    handleSelectionChange(selection) {
      this.userIds = selection.map(item => item.userId);
    },

    // 完成：选择物料
    selectOrderDetail_done(user) {
      console.log(user)
      this.dialog_selectOrderDetail.visible = false;
      this.$emit("ok", user );
    },


    // 对话框：显示 - 选择订单
    dialog_show_selectOrderDetail() {
      this.getList_orderDetails()
      this.dialog_selectOrderDetail.visible = true;
      this.dialog_selectOrderDetail.title = "选择订单明细";
    },

    // 对话框：隐藏 - 选择物料
    dialog_hide_selectOrderDetail() {
      this.dialog_selectOrderDetail.visible = false;
    },


    // 显示弹框
    show() {
      this.getList_orderDetails();
      this.visible = true;
    },
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row);
    },
    /**读取：订单明细*/
    selectOrder_done(scope){
      console.log(scope)
      this.dialog_selectOrderDetail.visible = false;
      this.$emit("ok", scope );
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList_orderDetails();
    },


    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },


  }
};
</script>
